import store from '../../store'
import { ElMessage } from '_element-plus@1.0.2-beta.48@element-plus'

export default {
  data () {
    var id
    var username = ''
    var password = ''
    var check = ''
    return {
      id,
      username,
      password,
      check
    }
  },
  methods: {
    register () {
      if (this.username === '' || this.password === '' || this.check === '') {
        ElMessage.error('用户名或密码不能为空')
        return
      }
      if (this.password !== this.check) {
        ElMessage.error('两次输入密码不一致')
        return
      }
      var userInfo = {
        username: this.username,
        password: this.password
      }
      store.dispatch('register', userInfo)
        .then(response => {
          const data = response.data
          if (data.message === 'success') {
            ElMessage({
              type: 'success',
              message: '注册成功'
            })
            this.$router.push({ name: 'login' })
          } else {
            ElMessage.error('用户名已存在')
          }
        })
        .catch(error => {
          console.log('connect failed')
          console.log(error)
        })
    }
  }
}
